<template>
	<view class="order _width">

		<view class="inner _width">
			<view class=" payInfo _width">
				<view class="lebel _width flex">{{order.type_text}}</view>
				<view class="money _width flex">¥{{order.money}}</view>
				<view class="isExpired" v-if="isExpired">
					订单已失效
				</view>
				<view class="pleaserHorder _width " v-else>
					请您在
					<view class="time">{{remainingTime}} </view>
					内完成支付，否则订单将会被自动取消。
				</view>
			</view>

			<view class="card orderDetails _width flex">
				<view class="_width label ">费用备注</view>
				<view class="_width label desc">{{order.remark}}</view>
			</view>

			<view class="card orderDetails _width flex">
				<view class="_width label " style="margin-bottom: 30rpx;">装修订单详情</view>
				<view class="row _width flex">
					<view class=" label left">需求编码：</view>
					<view class=" text right rt">{{demand.code}}</view>
				</view>

				<view class="row _width flex">
					<view class=" label left">需求分类：</view>
					<view class=" text right2 rt flex">
						<view v-for="(item2,index2) in demand.cat" :key="index" class="cat">{{item2.name}}</view>
					</view>
				</view>


				<view class="row _width flex">
					<view class=" label left">发布时间：</view>
					<view class=" text right rt">{{demand.create_time}}</view>
				</view>
				<view class="row _width flex">
					<view class=" label left">所在地区：</view>
					<view class=" text right rt">{{demand.city_name}} {{demand.county_name}}</view>
				</view>

				<view class="row _width flex">
					<view class=" label left">备注：</view>
					<view class=" text right ">{{demand.desc}}</view>
				</view>


			</view>


			<view class="card orderDetails _width flex">
				<view class="_width logLabels ">需求动态</view>
				<view class="log">
					<view class="for">
						<view class="line _width" v-for="(item3,index3) in demand.log" :key="index3">
							<view class="left">
							</view>
							<view class="right">
								<view class="row">
									<text class="time">{{item3.create_time}}</text>
									<view class="ImageArr rowBag" v-if="item3.type == 1">
										<text class="ImageArrLabel">{{item3.type_text}}</text>
									</view>

									<view class="ImageArr rowBag" v-if="item3.desc">
										<text class="ImageArrLabel">{{item3.desc}}</text>
									</view>


								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
		<!-- 去支付 -->
		<view class="Btn" v-if="!isExpired">
			<button type="primary" @click="payClick" class="payClick">支付</button>
		</view>

	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		onUnmounted,
		defineExpose,
		watch
	} from 'vue';
	import {
		onLoad,
		onReady,
		onShow,
		onHide,
	} from '@dcloudio/uni-app'
	import {
		demand_order,
		order_pay
	} from '@/api/require.js';

	const demand = ref({})
	const order = ref({})
	const logDetails = ref({})


	const remainingTime = ref(""); // 剩余时间的显示
	const isExpired = ref(false); // 是否过期
	const targetTime = ref(''); // 截止时间
	let timer = null; // 定时器引用

	const payData = ref({}) //支付参数

	const params = ref({
		id: '',
	})
	onLoad((option) => {
		if (option.id) {
			params.value.id = option.id
			// queryPayData();
		}
	})

	onShow(() => {
		if (params.value.id) {
			uni.$on('refreshIfNeeded', function(dat) {
				if (dat && !hasTriggered) {
					queryPayData()
				}
				uni.$off('refreshIfNeeded')
			})
			queryPayData()
		}
	})


	onMounted(() => {
		queryPayData()
	});

	onUnmounted(() => {
		clearInterval(timer); // 页面卸载时清除定时器
	});

	// 订单详情
	const queryPayData = async () => {
		const res = await demand_order({
			order_id: params.value.id
		});
		if (res.code === 1) {
			order.value = res.data.order;
			targetTime.value = convertToIOSCompatibleFormat(res.data.order.auto_cancel);
			demand.value = res.data.demand;
			startCountdown(); // 页面加载时启动倒计时
		} else {
			uni.showToast({
				icon: 'none',
				title: res.msg
			})
		}
	}


	// 将目标时间转换为兼容 iOS 的格式
	const convertToIOSCompatibleFormat = (time) => {
		// 假设返回的时间格式为 "yyyy-MM-dd HH:mm:ss"
		if (time) {
			// 替换空格为T，变为 "yyyy-MM-ddTHH:mm:ss" 格式
			return time.replace(" ", "T");
		}
		return time; // 如果已经是合适的格式，则直接返回
	};



	const calculateRemainingTime = () => {
		const now = new Date();
		const endTime = new Date(targetTime.value); // 使用正确的格式进行日期转换
		const timeDifference = endTime - now;

		if (timeDifference <= 0) {
			isExpired.value = true;
			clearInterval(timer); // 停止倒计时
			remainingTime.value = "订单已失效";
			return;
		}

		const hours = Math.floor(timeDifference / (1000 * 60 * 60));
		const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
		const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);

		remainingTime.value = `${hours} 小时 ${minutes} 分 ${seconds} 秒`;
	};



	// 启动倒计时
	const startCountdown = () => {
		calculateRemainingTime();
		timer = setInterval(calculateRemainingTime, 1000);
	};

	// 支付
	const payClick = async () => {
		const res = await order_pay({
			order_id: params.value.id
		});
		if (res.code === 1) {
			console.log('支付参数', res.data)
			payData.value = JSON.parse(res.data.payInfo)
			uni.setStorageSync('isShare', '1');
			await uni.requestPayment({
				provider: 'wxpay',
				timeStamp: payData.value.timeStamp,
				nonceStr: payData.value.nonceStr,
				package: payData.value.package,
				signType: payData.value.signType,
				paySign: payData.value.paySign,
				success: function(res) {
					uni.setStorageSync('isUpload', '1')
					uni.showToast({
						icon: 'success',
						title: res.msg,
					})
					setTimeout(() => {
						// uni.$emit('refreshIfNeeded', true)
						// uni.navigateBack({
						// 	delta: 1
						// })
						uni.navigateTo({
							url: '/pages/demand/demandStatus/index?status=' + 1
						});
					}, 200);
				},
				fail: function(err) {
					uni.setStorageSync('isUpload', '1')
					setTimeout(() => {
						// uni.$emit('refreshIfNeeded', true)
						// uni.navigateBack({
						// 	delta: 1
						// })
						uni.navigateTo({
							url: '/pages/demand/demandStatus/index?status=' + 1
						});


					}, 200);
				}
			});
		} else {
			uni.showToast({
				icon: 'none',
				title: res.msg,
			})
			// setTimeout(() => {
			// 	// uni.$emit('refreshIfNeeded', true)
			// 	// uni.navigateBack({
			// 	// 	delta: 1
			// 	// })
			// 	uni.navigateTo({
			// 		url: '/pages/demand/demandStatus/index?status=' + 1
			// 	});
			// }, 200);


		}
	}
</script>

<style lang="less" scoped>
	.order {
		height: 100vh;
		// overflow-y: auto;

		padding: 0 32rpx 120rpx;
		box-sizing: border-box;
		background: #F2F2F2;

		.inner {
			overflow: auto;
			height: calc(100% - 60rpx);

			.card {
				background: #FFFFFF;
				padding: 20rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				box-sizing: border-box;

				.logLabels {
					height: 80rpx;
					display: flex;
					align-items: center;
				}

				.log {
					padding: 20rpx;
					box-sizing: border-box;

					.line {
						display: flex;

						.left {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 26rpx;
							color: #666666;
							line-height: 36rpx;
							text-align: center;
							font-style: normal;
							text-transform: none;
							border-right: 1rpx solid #C8C8C8;
							padding-top: 20rpx;
							padding-right: 20rpx;
							position: relative;
							padding-bottom: 30rpx;
							box-sizing: border-box;

							&::before {
								content: '';
								position: absolute;
								right: 0;
								top: 30rpx;
								width: 20rpx;
								height: 20rpx;
								transform: translateX(50%);
								background: #FFFFFF;
								border-radius: 0rpx 0rpx 0rpx 0rpx;
								border: 4rpx solid #D5A969;
								border-radius: 50%;
								background-color: #fff;
							}
						}

						.right {
							padding-top: 20rpx;
							padding-left: 30rpx;
							display: flex;
							flex-wrap: wrap;
							width: calc(100% - 60rpx);

							.row {
								display: flex;
								flex-wrap: wrap;
								justify-content: flex-start;
								width: 100%;
								box-sizing: border-box;

								.time {
									font-family: PingFang SC, PingFang SC;
									font-weight: 500;
									font-size: 30rpx;
									color: #333333;
									line-height: 36rpx;
									text-align: left;
									font-style: normal;
									text-transform: none;
									width: 100%;
									margin-bottom: 20rpx;
								}

								.btnWrap {

									width: 100%;
									display: flex;
									justify-items: flex-start;

									.add {
										width: 180rpx !important;
										height: 65rpx;
										font-family: PingFang SC, PingFang SC;
										font-weight: 500;
										font-size: 28rpx;
										color: #FFFFFF;
										text-align: center;
										line-height: 65rpx;
										font-style: normal;
										text-transform: none;
										background: #D5A969;
										border-radius: 8rpx 8rpx 8rpx 8rpx;
									}
								}



								.rowBag {
									background: #F5F5F5;
									border-radius: 16rpx 16rpx 16rpx 16rpx;
									padding: 30rpx 25rpx;
									box-sizing: border-box;
									margin: 15rpx 0 40rpx;
								}

								.desc {
									font-family: PingFang SC, PingFang SC;
									font-weight: 400;
									font-size: 24rpx;
									color: #999999;
									line-height: 36rpx;
									text-align: left;
									font-style: normal;
									text-transform: none;
									width: 100%;
									margin-top: 20rpx;
									width: 100%;
									word-wrap: break-word;
									word-break: break-all;

								}

								.tagArr {
									width: 100%;
									display: flex;
									flex-wrap: wrap;


									.tag {
										width: 100%;
										margin-bottom: 20rpx;
										font-family: PingFang SC, PingFang SC;
										font-weight: 500;
										font-size: 26rpx;
										color: #D5A969;
										text-align: left;
										font-style: normal;
										text-transform: none;
										box-sizing: border-box;
										word-break: break-all;

										&:last-child {
											margin-bottom: 0 !important;
										}
									}



								}

								.ImageArr {
									width: 100%;
									display: flex;
									flex-wrap: wrap;

									.ImageArrLabel {
										font-family: PingFang SC, PingFang SC;
										font-weight: 400;
										font-size: 24rpx;
										color: #999999;
										line-height: 36rpx;
										text-align: left;
										font-style: normal;
										text-transform: none;
										width: 100%;
										word-break: break-all;

									}

									::v-deep .u-album {
										margin-top: 25rpx;
										width: 100%;

										.u-album__row__wrapper {}

										.u-album__row__wrapper {
											background-color: #C8C8C8;
											border-radius: 10rpx;
											overflow: hidden;
											width: 150rpx;
											height: 150rpx;

											image {
												width: 100% !important;
												height: 100% !important;
												object-fit: cover;
											}
										}
									}

									// image {
									// 	width: 100rpx;
									// 	height: 100rpx;
									// 	margin-right: 20rpx;
									// 	margin-bottom: 20rpx;
									// 	background-color: #C8C8C8;
									// 	border-radius: 20rpx;
									// 	overflow: hidden;

									// 	&:last-child {
									// 		margin-bottom: 0 !important;
									// 	}
									// }


								}


							}
						}
					}
				}

			}

			.payInfo {
				flex-wrap: wrap;

				.lebel {
					display: flex;
					justify-content: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;
					line-height: 36rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin: 15rpx 0 20rpx;
				}

				.money {
					display: flex;
					align-items: center;
					justify-content: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 40rpx;
					color: #2F2F2F;
					line-height: 36rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin-bottom: 20rpx;

				}

				.isExpired {
					text-align: center;
					color: #666666;
				}

				.pleaserHorder {
					padding: 10rpx 20%;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;
					line-height: 36rpx;
					text-align: center;
					font-style: normal;
					text-transform: none;
					font-style: normal;
					box-sizing: border-box;
					white-space: normal;
					word-wrap: break-word;
					display: block;

					.time {
						color: #FF3535;
						width: 220rpx;
						text-align: center;
						display: inline-block;
					}
				}

			}



			.orderDetails {
				margin-top: 20rpx;
				flex-wrap: wrap;

				text {
					display: block;
				}

				.label {

					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 500;
					font-size: 30rpx;
					color: #333333;
					line-height: 36rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
				}

				.desc {
					width: 100%;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #333333;
					line-height: 36rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin-top: 25rpx;
					word-wrap: break-word;
					word-break: break-all;
				}

				.row {
					// border: 1px solid red;
					box-sizing: border-box;
					margin-bottom: 20rpx;

					.left {
						width: 160rpx;
						font-size: 28rpx !important;
						font-weight: 400 !important;
						color: #333333 !important;
					}

					.rt {
						text-align: right;
						display: flex;
						justify-content: flex-end;
						color: #666666;

					}

					.right2 {
						width: calc(100% - 160rpx);
						padding: 0 10rpx;
						word-wrap: break-word;
						word-break: break-all;
						box-sizing: border-box;

						.cat {
							display: flex;
							margin-left: 10rpx;

							&:first-child {
								margin-left: 0rpx;
							}
						}
					}

					.right {
						width: calc(100% - 160rpx);
						display: flex;
						text-align: right;
						padding: 0 10rpx;
						word-wrap: break-word;
						word-break: break-all;
						color: #666666;

					}
				}

			}
		}


	}

	.Btn {
		height: calc(120rpx + env(safe-area-inset-bottom));
		height: calc(120rpx + constant(safe-area-inset-bottom));
		width: 100%;
		padding: 20rpx;
		height: 120rpx;
		box-shadow: 0rpx -4rpx 6rpx 0rpx rgba(0, 0, 0, 0.05);
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;

		button {
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			font-size: 34rpx;
			color: #FFFFFF;
			line-height: 88rpx;
			text-align: center;
			font-style: normal;
			text-transform: none;
		}

		.payClick {
			width: 100%;
			height: 88rpx;
			background: linear-gradient(115deg, #F2BD6F 0%, #CCA05F 100%);
			border-radius: 20rpx 20rpx 20rpx 20rpx;
		}

		.isClick {
			opacity: 0.5;
		}



	}
</style>